package yang.learn.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yang.learn.entity.User;
import yang.learn.service.UserService;

import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired(required = false)
    private UserService userService;

    @ResponseBody
    @RequestMapping("register")
    public ModelAndView register(@RequestBody User user) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("register");
        System.out.println("注册用户信息为"+user);
        User queryAccount = userService.queryAccount(user);
        if(queryAccount==null){
            userService.add(user);
        }else{
            user.setStats("用户账号已被注册");
        }
        modelAndView.addObject(user);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("login")
    public ModelAndView login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        User queryAccount = userService.queryAccount(user);
        System.out.println("登录用户："+queryAccount);
        if(queryAccount==null){
            user.setStats("用户账号不存在！");
        }else{
            if(queryAccount.getUser_password().equals(user.getUser_password())){
                //生成一个token
                String token = UUID.randomUUID().toString();
                System.out.println("用户登录成功，获得token:" + token);
                HttpSession session = request.getSession();
                //服务端备份token
                session.setAttribute("token", token);
                //服务端备份用户信息
                session.setAttribute("user_id", queryAccount.getUser_id());
                session.setAttribute("user_name", queryAccount.getUser_name());
                //客户端存储token
                Cookie cookie = new Cookie("uuid",token);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }
        return modelAndView;
    }

    @RequestMapping("loginOut")
    public ModelAndView loginOut(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        HttpSession session = request.getSession();
        //清空token
        session.removeAttribute("token");
        for (Cookie cookie:request.getCookies()) {
            if("uuid".equals(cookie.getName())){
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
        User user = new User();
        user.setUser_name("未登录(点击登录)");
        modelAndView.addObject("context", "hello.html");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("manage")
    public String manage(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User queryId = userService.queryId(new User(
                Integer.parseInt(String.valueOf(session.getAttribute("user_id")))));
        queryId.setUser_password("");
        model.addAttribute("user",queryId);
        return "user.html :: user";
    }

    @RequestMapping("change")
    public ModelAndView change(User user, HttpServletRequest request, String new_password, String true_password) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("change");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(String.valueOf(session.getAttribute("user_id")));
        user.setUser_id(user_id);
        System.out.println("修改用户信息为"+user);
        userService.change(user,new_password,true_password);
        user.setUser_password("");
        modelAndView.addObject("user",userService.queryId(user));
        return modelAndView;
    }

}
